package cn.edu.swu.dlx.controller;

import cn.edu.swu.dlx.db.DatabaseService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;

@WebServlet(urlPatterns = "/deleteBook")
public class DeleteBook extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        DatabaseService service = (DatabaseService)request.getServletContext().getAttribute(DatabaseService.CONTEXT_KEY);

        try {
            service.execute("delete from book where id=" + id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        response.sendRedirect("./books");
    }
}
